using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics.PackedVector;


namespace DomoBCI._3D
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Camera : Microsoft.Xna.Framework.GameComponent
    {
        public Matrix view { get; protected set; }
        public Matrix projection { get; protected set; }
        public Vector3 cameraPosition { get; protected set; }
        Vector3 cameraDirection;
        Vector3 cameraUp;
        float speed = 0.1f;
        MouseState mPrevs, mCurrs;


        public Camera(Game game, Vector3 pos, Vector3 target, Vector3 up)
            : base(game)
        {
            //Inizializzo view
            //view = Matrix.CreateLookAt(pos, target, up);
            cameraPosition = pos;
            cameraDirection = target - pos;
            cameraDirection.Normalize();
            cameraUp = up;
            CreateLookAt();
            //Inizializzo projection
            projection = Matrix.CreatePerspectiveFieldOfView(
          MathHelper.PiOver4,
          (float)Game.Window.ClientBounds.Width /
          (float)Game.Window.ClientBounds.Height,
          1, 30000);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            Mouse.SetPosition(Game.Window.ClientBounds.Width / 2, Game.Window.ClientBounds.Height / 2);
            mPrevs = Mouse.GetState();

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                cameraPosition += cameraDirection * speed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                cameraPosition -= cameraDirection * speed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                cameraPosition += Vector3.Cross(cameraUp, cameraDirection) * speed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                cameraPosition -= Vector3.Cross(cameraUp, cameraDirection) * speed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.NumPad4))
            {
                cameraDirection -= new Vector3(0.1f, 0, 0);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.NumPad6))
            {
                cameraDirection += new Vector3(0.1f, 0, 0);
            }
            // pitch
            cameraDirection = Vector3.Transform(
                cameraDirection,
                Matrix.CreateFromAxisAngle(
                    cameraUp,
                    (-MathHelper.PiOver4 / 148) * (Mouse.GetState().X - mPrevs.X)
                    )
               );
            // roll
            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                cameraUp = Vector3.Transform(
                    cameraUp,
                    Matrix.CreateFromAxisAngle(
                    cameraDirection, MathHelper.PiOver4 / 45));
            }

            //if (Mouse.GetState().RightButton == ButtonState.Pressed)
            //{
            //    cameraUp = Vector3.Transform(
            //        cameraUp,
            //        Matrix.CreateFromAxisAngle(
            //        cameraDirection, -MathHelper.PiOver4 / 45));
            //}

            cameraDirection = Vector3.Transform(
            cameraDirection,
            Matrix.CreateFromAxisAngle(
                Vector3.Cross(cameraUp, cameraDirection),
                (MathHelper.PiOver4 / 100) * (Mouse.GetState().Y - mPrevs.Y)
                )
            );

            cameraUp = Vector3.Transform(
                    cameraUp,
                    Matrix.CreateFromAxisAngle(
                        Vector3.Cross(cameraUp, cameraDirection),
                        (MathHelper.PiOver4 / 100) * (Mouse.GetState().Y - mPrevs.Y)
                        )
                    );
            mPrevs = Mouse.GetState();
            CreateLookAt();
            base.Update(gameTime);
        }

        private void CreateLookAt()
        {
            view = Matrix.CreateLookAt(cameraPosition, cameraPosition + cameraDirection, cameraUp);
        }
    }
}
